package org.hc.controller;

import com.baomidou.mybatisplus.plugins.Page;
import org.hc.domain.Sso;
import org.hc.query.SsoQuery;
import org.hc.service.ISsoService;
import org.hc.util.AjaxResult;
import org.hc.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sso")
public class SsoController {
    @Autowired
    public ISsoService ssoService;


    @GetMapping("/ac/{accessToken}")
    public Sso querySso(@PathVariable("accessToken") String accessToken){
        System.out.println(accessToken);
        return ssoService.querySso(accessToken);
    }
    //登录
    @PostMapping("/login")
    /**
     * phone:'13330964748',
     password:'123456',
     */
    public AjaxResult login(@RequestBody Sso sso){
        return ssoService.login(sso);
    }

    //注册
    @PostMapping("/register")
    /**
     * mobile:'13330964748',
     password:'123456',
     imageCode:'',
     smsCode:''
     ==============Map接口---不是通过feign,通过zuul转发过来外部请求
     */
    public AjaxResult register(@RequestBody Map<String,String> params)
    {
        return ssoService.register(params);
    }

    /**
     * 保存和修改公用的
     * @param sso  传递的实体
     * @return Ajaxresult转换结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody Sso sso){
        try {
            ssoService.insert(sso);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            ssoService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
     * 保存和修改公用的
     * @param sso  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Sso sso){
        try {
            if(sso.getId()!=null){
                ssoService.updateById(sso);
            }else{
                ssoService.insert(sso);
            }

            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Sso get(@PathVariable("id")Long id)
    {
        return ssoService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     * @return
     */
    @GetMapping
    public List<Sso> list(){

        return ssoService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Sso> json(@RequestBody SsoQuery query)
    {
        Page<Sso> page = new Page<Sso>(query.getPage(),query.getRows());
        page = ssoService.selectPage(page);
        return new PageList<Sso>(page.getTotal(),page.getRecords());
    }
}
